package com.tsugun.mediaplayer.view.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

import java.lang.ref.WeakReference;

/**
 * 图片切换器
 * Created by shize on 2017/4/7.
 */

public class ViewSwitcher extends ViewPager {
    private static final long SWITCHER_DELAYED_TIME = 5000;
    private static final int MESSAGE_SWITCHER_VIEW = 0x10;
    // 存放子View
    private SparseArray<View> mChildrenViews;
    private SwitcherHandler mHandler;
    // 当前子项
    private int mCurrentItem;
    private boolean isTouch;
    private boolean isClose;

    public ViewSwitcher(Context context) {
        this(context, null);
    }

    public ViewSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData();
    }

    private void initData() {
        isClose = false;
        mCurrentItem = getCurrentItem();
        mChildrenViews = new SparseArray<>();
        mHandler = new SwitcherHandler(this);
        mHandler.sendEmptyMessageDelayed(MESSAGE_SWITCHER_VIEW, SWITCHER_DELAYED_TIME);
    }

    @Override
    protected void onPageScrolled(int position, float offset, int offsetPixels) {
        super.onPageScrolled(position, offset, offsetPixels);
    }

    public void setViewForPosition(View v, int position) {
        mChildrenViews.put(position, v);
    }

    public View findViewByPosition(int pos) {
        return mChildrenViews.get(pos);
    }

    /**
     * @param isTouch 是否正在触发交互
     */
    public void isTouch(boolean isTouch) {
        this.isTouch = isTouch;
    }

    /**
     * 自动切换方法
     */
    private void switchCurrentItem() {
        if (!isTouch && !isClose) {
            mCurrentItem++;
            if (mCurrentItem < 5) {
                arrowScroll(View.FOCUS_RIGHT);
            } else {
                mCurrentItem = 0;
                setCurrentItem(0);
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // 关闭handler
        mHandler.removeCallbacksAndMessages(null);
    }

    public void close() {
        isClose = true;
    }

    private static class SwitcherHandler extends Handler {

        private WeakReference<ViewSwitcher> mSwitcher;

        SwitcherHandler(ViewSwitcher viewSwitcher) {
            super();
            mSwitcher = new WeakReference<>(viewSwitcher);
        }

        @Override
        public void handleMessage(Message msg) {
            ViewSwitcher viewSwitcher = mSwitcher.get();
            if (viewSwitcher == null) {
                return;
            }
            switch (msg.what) {
                case MESSAGE_SWITCHER_VIEW:
                    viewSwitcher.switchCurrentItem();
                    sendEmptyMessageDelayed(MESSAGE_SWITCHER_VIEW, SWITCHER_DELAYED_TIME);
                    break;
            }
        }
    }
}
